package com.baizhi.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baizhi.entity.Comment;
import com.baizhi.service.CommentService;
import com.baizhi.service.impl.CommentServiceImpl;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommentAction {
    private Comment c;

    public Comment getC() {
        return c;
    }

    public void setC(Comment c) {
        this.c = c;
    }

    public void selectAll() throws IOException {
        System.out.println("查询action");
        CommentService cs = new CommentServiceImpl();
        List<Comment> list = cs.selectAll();
        //将list集合封装到 json字符串中
        String s = JSON.toJSONString(list);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        //将封装好的json字符串响应给浏览器
        pw.print(s);
        pw.flush();

    }
    public void addComment() throws IOException {
        System.out.println("添加action");
        CommentService cs = new CommentServiceImpl();
        cs.addComment(c);
        System.out.println("添加后的对象:"+c);
        //添加之后   调用查询全部的方法
        selectAll();
    }
    public void addComment2() throws IOException {
        System.out.println("添加action2");
        CommentService cs = new CommentServiceImpl();
        cs.addComment(c);
        System.out.println("添加后的对象的id:"+c.getId());
        System.out.println("添加后的对象:"+c);
        //添加之后   将添加的对象封装成JSON字符串  响应给页面
        String s = JSON.toJSONString(c);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        //将封装好的json字符串响应给浏览器
        pw.print(s);
        pw.flush();
    }
    public void updateComment() throws IOException {
        System.out.println("更新action的参数："+c.getId());
        CommentService cs = new CommentServiceImpl();
        cs.updateComment(c.getId());
        //更新之后   调用查询一个的方法
        Comment comment = cs.selectOne(c.getId());
        HttpServletResponse response = ServletActionContext.getResponse();
        PrintWriter pw = response.getWriter();
        //将更新之后的点赞值响应给页面
        pw.print(comment.getZan());
        System.out.println("更新后的点赞值："+comment.getZan());
        pw.flush();
    }

    public void add(){
        CommentService cs = new CommentServiceImpl();
        cs.addComment(c);
        Map<String,Object> map = new HashMap<>();
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = null;
        try {
            map.put("flag",true);
            map.put("comment",c);
            pw = response.getWriter();
        } catch (IOException e) {
            map.put("flag",false);
            e.printStackTrace();
        }
        //将封装好的json字符串响应给浏览器
        String s = JSONObject.toJSONString(map);
        pw.print(s);
        pw.flush();
    }
    public void update(){
        CommentService cs = new CommentServiceImpl();
        cs.updateComment(c.getId());
        Map<String,Object> map = new HashMap<>();
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = null;
        try {
            map.put("flag",true);
//            map.put("cid",c.getId());
            pw = response.getWriter();
        } catch (IOException e) {
            map.put("flag",false);
            e.printStackTrace();
        }
        //将封装好的json字符串响应给浏览器
        String s = JSONObject.toJSONString(map);
        System.out.println(s);
        pw.print(s);
        pw.flush();
    }
}
